package com.goke.media.call;

//import android.app.Activity;
//import org.webrtc.webrtcdemo.MainMenuFragment;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
//import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
//import android.support.v7.app.AppCompatActivity;

import com.goke.media.R;
import com.goke.media.R.id;
import com.goke.media.R.layout;
import com.goke.media.call.CallActivity;

import java.util.ArrayList;
import java.util.List;

public class LoginActivity extends Activity {

	private String username, password, testmode;
	private String version = "Author: chival366\n"
			+ "E-mail: 794960056@qq.com\n" + "Source: github.com/chival366\n"
			+ "Date:   2020/2/25 19:00";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		final TextView versionEditText = (TextView) findViewById(R.id.textViewVersion);
		final EditText usernameEditText = (EditText) findViewById(R.id.username);
		final EditText passwordEditText = (EditText) findViewById(R.id.password);
		final Button loginButton = (Button) findViewById(R.id.login);
        
		versionEditText.setText(version);
		testmode = "0";
		
		loginButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				/*
				 * loadingProgressBar.setVisibility(View.VISIBLE);
				 * loginViewModel.login(usernameEditText.getText().toString(),
				 * passwordEditText.getText().toString());
				 */

				username = usernameEditText.getText().toString();
				password = passwordEditText.getText().toString();
				setupCallActivity(username, password);
			}
		});
	}

	private void setupCallActivity(String username, String password) {

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
			checkMultiplePermissions();
		}
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_call, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings0) {
			Toast.makeText(this, "Audio LAN Call: Only Audio in LAN", Toast.LENGTH_SHORT)
					.show();
			testmode = "0";
		}
		else if (id == R.id.action_settings1) {
			Toast.makeText(this, "Audio WAN Call: Only Audio in WAN",
					Toast.LENGTH_SHORT).show();
			testmode = "1";
		}
		else if (id == R.id.action_settings2) {
			Toast.makeText(this, "Video Call: Audio And Video in LAN",
					Toast.LENGTH_SHORT).show();
			testmode = "2";
		}
		else if (id == R.id.action_settings3) {
			Toast.makeText(this, "Video Call: Audio And Video in WAN",
					Toast.LENGTH_SHORT).show();
			testmode = "3";
		}
		return super.onOptionsItemSelected(item);
	}

	private  final int REQUEST_PERMISSION_CODE = 0;
	/*private  final int REQUEST_PERMISSION_RECORD_CODE = 1;
	private  final int REQUEST_PERMISSION_CAMERA_CODE = 2;
	private  final int REQUEST_PERMISSION_STORAGE_CODE = 3;*/

	@SuppressLint("NewApi")
	private boolean addPermission(List<String> permissionsList, String permission) {

		if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {

			permissionsList.add(permission);

			if (!shouldShowRequestPermissionRationale(permission))
				return false;
		}
		return true;
	}

	@SuppressLint("NewApi")
	private void checkMultiplePermissions() {
		List<String> permissionsNeeded = new ArrayList<String>();

		final List<String> permissionsList = new ArrayList<String>();
		if (!addPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE))
			permissionsNeeded.add("WRITE_EXTERNAL_STORAGE");
		if (!addPermission(permissionsList, Manifest.permission.CAMERA))
			permissionsNeeded.add("CAMERA");
		if (!addPermission(permissionsList, Manifest.permission.RECORD_AUDIO))
			permissionsNeeded.add("RECORD_AUDIO");
		if (permissionsList.size() > 0) {
			if (permissionsNeeded.size() > 0) {

				String message = "must open pemissions:  " + permissionsNeeded.get(0);
				for (int i = 1; i < permissionsNeeded.size(); i++) {
					message = message + ", " + permissionsNeeded.get(i);
				}
				Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
				requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), REQUEST_PERMISSION_CODE);
				return;
			}
			requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), REQUEST_PERMISSION_CODE);
			return;
		}
	}

	@SuppressLint("NewApi")
	@Override
	public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
		Log.i("permission", "onRequestPermissionsResult requestCode=" + requestCode + " permissions " + permissions[0] + " grantResults=" + grantResults.length);
		if (requestCode == REQUEST_PERMISSION_CODE) {
			// 相关权限开启与否均需执行initapi操作，
			Log.i("permission", "REQUEST_PERMISSION_CODE");

			for (int i = 0; i < grantResults.length; i++) {
				if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
					continue;
				} else {
					Toast.makeText(LoginActivity.this, permissions[i] + "Permission not open", Toast.LENGTH_SHORT).show();
				}
			}

			Intent intent = new Intent(this, CallActivity.class);
			intent.putExtra("username", username);
			intent.putExtra("password", password);
			intent.putExtra("testmode", testmode);

			startActivity(intent);
		}
	}
}
